package org.lyimg.com.imgapplication.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import org.lyimg.com.imgapplication.R;
import org.lyimg.com.imgapplication.adapter.ImgRecAdapter;
import org.lyimg.com.imgapplication.bean.ImgList;
import org.lyimg.com.imgapplication.presenter.ImgsPresente;
import org.lyimg.com.imgapplication.utils.SpacesItemDecoration;
import org.lyimg.com.imgapplication.view.IimgsView;

import java.util.ArrayList;
import java.util.List;

public class ImgsActivity extends BaseActivity<IimgsView,ImgsPresente> implements IimgsView {

    RecyclerView imgs_recycler;
    SwipeRefreshLayout refresh;
    List<ImgList> list;
    ImgRecAdapter adapter;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imgs);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_imgs);
        setSupportActionBar(toolbar);

        assert toolbar != null;
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        refresh = findViewById(R.id.refresh);
        imgs_recycler =  findViewById(R.id.imgs_recycler);
        //设置布局管理器
        imgs_recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //设置Item增加、移除动画
        imgs_recycler.setItemAnimator(new DefaultItemAnimator());
        SpacesItemDecoration decoration = new SpacesItemDecoration(10);
        imgs_recycler.addItemDecoration(decoration);

        type = getIntent().getStringExtra("type");
        list = new ArrayList<>();
        adapter = new ImgRecAdapter(ImgsActivity.this, list,"https://m.aitaotu.com" + type);
        imgs_recycler.setAdapter(adapter);

        presenter.setUrl(type);
        presenter.getImgs(1);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getImgs(1);
            }
        });
        imgs_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                boolean nodown = imgs_recycler.canScrollVertically(1);
                if (!nodown) {
                    presenter.getImgs(2);
                }
            }
        });
    }

    @Override
    protected ImgsPresente creatPresenter() {
        return new ImgsPresente();
    }

    @Override
    public void showLoadingDialog() {
        showDialog();
    }

    @Override
    public void dissLoadingDialog() {
        dismissDialog();
    }

    @Override
    public void showError(String msg) {
        Log.i("TAG", "showError: " + msg);
//        Showtosat(msg);
    }

    @Override
    public void notifyImgList(int page, List<ImgList> imgs) {
        if (page == 1) {
            list.clear();
        }
        list.addAll(imgs);
        adapter.notifyDataSetChanged();
    }
}
